//
//  ViewController.m
//  自作始终
//
//  Created by Jorn.Wu on 15/6/30.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "ViewController.h"

#define imageHeight _clockView.bounds.size.height
#define imageWidth _clockView.bounds.size.width
//每秒秒针转动的角度
#define perSecondeA 360 / 60
//每分分针转动的角度
#define perMinuteA 360 / 60
//每小时时针转动的角度
#define perHourA 360 / 12
//角度转化
#define angleToRadian(x) (x) / 180 * M_PI


@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *clockView;
@property (nonatomic, strong) NSMutableArray *pictures;
@property (nonatomic, strong) NSArray *pictureNames;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, strong) CALayer *second;
@property (nonatomic, strong) CALayer *minute;
@property (nonatomic, strong) CALayer *hour;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;


- (IBAction)changeImage:(UIButton *)sender;


@end

@implementation ViewController
- (NSMutableArray *)pictures{
    if (_pictures == nil) {
        _pictures = [NSMutableArray array];
    }
    return _pictures;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.pictureNames = @[@"表盘1",@"表盘2",@"表盘3",@"表盘4",@"表盘5",@"表盘6",@"表盘7",@"表盘7 2",@"表盘7 3",@"表盘8",@"表盘8 2",@"表盘8 3"];
    _number = 0;
    // Do any additional setup after loading the view, typically from a nib.
    for (int i = 0; i < _pictureNames.count; i++) {
        UIImage *img = [UIImage imageNamed:_pictureNames[i]];
        [self.pictures addObject:img];
    }
    
    self.clockView.image = self.pictures[_number];
    
    [self addHourHand];
    [self addMinuteHand];
    [self addSecondHand];
    [self addCenterPoint];
    
    //开启计时器
    [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(update) userInfo:nil repeats:YES];
    
}

- (void)addSecondHand{
    _second = [CALayer layer];
    _second.bounds = CGRectMake(0, 0, 1, imageHeight * 0.5 - 5);
    _second.position = CGPointMake(_clockView.center.x, _clockView.center.y);
    _second.anchorPoint = CGPointMake(0.5, 0.9);
    _second.backgroundColor = [UIColor redColor].CGColor;
    
    [self.view.layer addSublayer:_second];
    
}
- (void)addCenterPoint{
    CALayer *cp = [CALayer layer];
    cp.bounds = CGRectMake(0, 0, 8, 8);
    cp.cornerRadius = 4;
    cp.position = CGPointMake(_clockView.center.x, _clockView.center.y);
    cp.anchorPoint = CGPointMake(0.5, 0.5);
    cp.backgroundColor = [UIColor brownColor].CGColor;
    
    [self.view.layer addSublayer:cp];
}
- (void)addMinuteHand{
    _minute = [CALayer layer];
    _minute.bounds = CGRectMake(0, 0, 3, imageHeight * 0.5 - 10);
    _minute.cornerRadius = 1.5;
    _minute.position = CGPointMake(_clockView.center.x, _clockView.center.y);
    _minute.anchorPoint = CGPointMake(0.5, 0.85);
    _minute.backgroundColor = [UIColor blueColor].CGColor;
    
    [self.view.layer addSublayer:_minute];
}

- (void)addHourHand{
    _hour = [CALayer layer];
    _hour.bounds = CGRectMake(0, 0, 5, imageHeight * 0.5 - 30);
    _hour.cornerRadius = 2.5;
    _hour.position = CGPointMake(_clockView.center.x, _clockView.center.y);
    _hour.anchorPoint = CGPointMake(0.5, 0.8);
    _hour.backgroundColor = [UIColor blueColor].CGColor;
    
    [self.view.layer addSublayer:_hour];
}

- (void)update{
    //获取日历
    NSCalendar *calender = [NSCalendar currentCalendar];
    //获取组件
    NSDateComponents *component = [calender components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];
    
    CGFloat sec = component.second;
    CGFloat min = component.minute;
    CGFloat hou = component.hour;
    
    self.timeLabel.text = [NSString stringWithFormat:@"%02.lf : %02.lf : %02.lf",hou,min,sec];
    
    if (hou > 12) {
        hou = hou - 12;
    }
    //各指针的角度
    CGFloat secondA = sec * perSecondeA;//秒针起始角度
    CGFloat minuteA = secondA / 60 + min * perMinuteA;//要加上零头
    CGFloat hourA = hou * perHourA + minuteA / 12;
    
    //调整指针的方向
    _second.transform = CATransform3DMakeRotation(angleToRadian(secondA), 0, 0, 1);
    _minute.transform = CATransform3DMakeRotation(angleToRadian(minuteA), 0, 0, 1);
    _hour.transform = CATransform3DMakeRotation(angleToRadian(hourA), 0, 0, 1);
}

- (IBAction)changeImage:(UIButton *)sender {
    _number ++;
    if (_number >= _pictures.count) {
        _number = 0;
    }
    
    self.clockView.image = self.pictures[_number];

}
@end
